cmd line tutorials - gencmd

cmd line tutorials - gencmd

The echo Command

Unix-Linux Mac

The echo command is a built-in shell command that prints the specified text to the standard output. It ca be used for:

  • Printing messages to the console.
  • Creating files.
  • Passing data to other commands.

Syntax

The syntax for the echo command is as follows:

echo [options] <text>

The <text> argument is the text that you want to print to the console. You can also use the -e option to enable escape sequences in the text.

Examples

Print a simple message to the console.

echo "Hello, world!"

Create a file by piping the output of the echo command to the > operator.

echo "This is a log message." > logfile.txt

Pass data to another command by piping the output of the echo command to the | operator.

echo "This is the data." | grep "data"

Options

The echo command has a few options that can be used to modify its behavior.

-e: Enable escape sequences in the text.

-n: Do not print a newline character at the end of the output.

For more information on the echo command and its options, please see the man page: man echo.

Additional tips

You can use the echo command to print multiple lines of text by separating the lines with backslashes (\).

You can use the echo command to print variables by enclosing the variable name in curly braces ({}).

You can use the echo command to format the output using escape sequences. For example, to print the text “hello world” in blue color, you would use the following command:

echo -e "\033[34m hello there\033[0m"

With gencmd

gencmd echo "hello world" in red color

  • echo -e “\e[31mhello world\e[0m”
  • echo -e “\033[31mhello world\033[0m”